﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace Common
{
    /// <summary>
    /// 利用正则表达式验证的类
    /// </summary> 
    public class RegularExp
    {        
       #region 常用正则表达式枚举类 
       public class RegExpPattern
      {
          /// <summary>
          /// 日期（2011-04-20）
          /// </summary> 
           public const string DateString = @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";


            /// <summary>
            /// 非负整数（正整数 + 0）
            /// </summary> 
          public const string NonNegativeNumber = @"^\d+$";

            /// <summary> 
            /// 非正整数（负整数 + 0） 
            /// </summary>
          /// <summary>
           /// 正整数    
            /// </summary>
            public const string PositiveNumber = @"^[0-9]*[1-9][0-9]*$";
          /// <summary>
            /// 负整数 
           /// </summary> 
            public const string NegativeNumber = @"^-[0-9]*[1-9][0-9]*$";
            /// <summary> 
            /// 整数  
            /// </summary> 
            public const string Number = @"^-?\d+$";
            /// <summary> 
            /// 非负浮点数（正浮点数 + 0）  
            /// </summary> 
            public const string NonNegativeFloat = @"^\d+(\.\d+)?$";
            /// <summary> 
            /// 正浮点数  
            /// </summary> 
            public const string PositiveFloat = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
            /// <summary> 
            /// 非正浮点数（负浮点数 + 0）  
            /// </summary> 
            public const string NonPositiveFloat = @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$";
            /// <summary> 
            /// 负浮点数  
            /// </summary> 
            public const string NegativeFloat = @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
            /// <summary> 
            /// 浮点数  
            /// </summary> 
            public const string Float = @"^(-?\d+)(\.\d+)?$";
            /// <summary> 
            /// 由26个英文字母组成的字符串  
            /// </summary> 
            public const string LetterString = @"^[A-Za-z]+$";
            /// <summary> 
            /// 由26个英文字母的大写组成的字符串  
            /// </summary> 
            public const string UpperLetterString = @"^[A-Z]+$";
            /// <summary> 
            /// 由26个英文字母的小写组成的字符串  
            /// </summary> 
            public const string LowerLetterString = @"^[a-z]+$";
            /// <summary> 
            /// 由数字和26个英文字母组成的字符串  
            /// </summary> 
            public const string DigitLetterString = @"^[A-Za-z0-9]+$";
            /// <summary> 
            /// 由数字、26个英文字母或者下划线组成的字符串 
            /// </summary> 
            public const string GeneralString = @"^\w+$";
            /// <summary> 
            /// 电话号码 
            /// </summary> 
            public const string TelephoneNumber = @"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?";
            /// <summary> 
            /// IP地址 
            /// </summary> 
            public const string IPAddr = @"^([1-9]|[1-9]\d|1\d{2}|2[0-1]\d|22[0-3])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$";
            /// <summary> 
            /// 匹配中文字符的正则表达式 
            /// </summary> 
            public const string ChineseString = @"[\一-\龥]";
            /// <summary> 
            /// 匹配双字节字符(包括汉字在内) 
            /// </summary> 
            public const string UnicodeText = @"[^\x00-\xff]";
            /// <summary> 
            /// 匹配空行的正则表达式 
            /// </summary> 
            public const string BlankLine = @"\n[\s| ]*\r";
            /// <summary> 
            /// 匹配HTML标记的正则表达式 
            /// </summary> 
            public const string HtmlFlag = @"/<(.*)>.*<\/\1>|<(.*) \/>/";
            /// <summary> 
            /// 匹配Email地址的正则表达式 
            /// </summary> 
            public const string EmailAddr = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            /// <summary> 
            /// 匹配网址URL的正则表达式 
            /// </summary> 
            public const string UrlAddr = @"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
            /// <summary> 
            /// 匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线) 
            /// </summary> 
            public const string BankAcct = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
            /// <summary> 
            /// 匹配国内电话号码 
            /// </summary> 
            public const string ChinaTelephone = @"(\d{3}-|\d{4}-)?(\d{8}|\d{7})?";
        }  
        #endregion 
        #region private function 
        private static bool IsMatched(string strInputText, string strRegPatter, RegexOptions regFlag)
        {
            Regex ex = new Regex(strRegPatter, regFlag);
            return ex.IsMatch(strInputText);
        }  
       #endregion 
       #region public function 
        /// <summary>
        /// 判断是否是一个合法的EMail地址
        /// </summary>
        public static bool IsMailAddr(string strInputText)
        {
            return IsMailAddr(strInputText, true);
        }
        public static bool IsMailAddr(string strInputText, bool bIgnoreCase)
        {
            RegexOptions regFlag = RegexOptions.Compiled;
            if (bIgnoreCase) regFlag = regFlag | RegexOptions.IgnoreCase;
            return IsMatched(strInputText, RegExpPattern.EmailAddr, regFlag);
        }
        /// <summary>
        /// 判断是否是一个合法的日期
        /// </summary>
        public static bool IsDateString(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.DateString, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个合法的IP地址
        /// </summary>
        public static bool IsIPAddr(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.IPAddr, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个数字
        /// </summary>
        public static bool IsNumber(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.Number, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个负数
        /// </summary>
        public static bool IsNegativeNumber(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.NegativeNumber, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个正数
        /// </summary>
        public static bool IsPositiveNumber(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.PositiveNumber, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个非负数
        /// </summary>
        public static bool IsNonNegativeNumber(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.NonNegativeNumber, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个浮点数（小数）
        /// </summary>
        public static bool IsFloat(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.Float, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个负浮点数（负小数）
        /// </summary>
        public static bool IsNegativeFloat(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.NegativeFloat, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个正浮点数（正小数）
        /// </summary>
        public static bool IsPositiveFloat(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.PositiveFloat, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个非负浮点数（非负小数）
        /// </summary>
        public static bool IsNonNegativeFloat(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.NonNegativeFloat, RegexOptions.Compiled);
        }
        /// <summary>
        /// 判断是否是一个非正浮点数（非正小数）
        /// </summary>
        public static bool IsNonPositiveFloat(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.NonPositiveFloat, RegexOptions.Compiled);            
        }
        /// <summary> 
        /// 判断帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线) 
        /// </summary> 
        public static bool IsBankAcct(string strInputText)
        {
            return IsMatched(strInputText, RegExpPattern.BankAcct, RegexOptions.Compiled);
        }
        /// <summary> 
        /// 查找字符串出现的次数（str被查找的字符串-regStr需要查找的字符串）
        /// </summary> 
        public static int MatcheCount(string str, string regStr)
        {
            Regex reg = new Regex(regStr, RegexOptions.Singleline);
            return reg.Matches(str).Count;
        }
        #endregion
   }


}
